export class GraphNode {
  r = 0;
  g = 0;
  b = 0;
  a = 0;
  x = 0;
  y = 0;
  // 前驱，后继，
  preNode = undefined;
  nextNode = undefined;
  upNode = undefined;
  downNode = undefined;
  constructor(opt) {
    this.r = opt.r;
    this.g = opt.g;
    this.b = opt.b;
    this.a = opt.a;
    this.x = opt.x;
    this.y = opt.y;
  }
  findNode(type, pos) {
    let node = this;

    while (true) {
      if (node === undefined) {
        return undefined;
      }
      if (node.x === pos.x && node.y === pos.y) {
        return node;
      }
      switch (type) {
        case "up":
          node = node.upNode;
          break;
        case "down":
          node = node.downNode;
          break;
        case "pre":
          node = node.preNode;
          break;
        case "next":
          node = node.nextNode;
          break;
      }
    }
  }
  appendNextNode(node) {
    this.nextNode = node;
  }
  appendPreNode(node) {
    this.preNode = node;
  }
  appendUpNode(node) {
    this.upNode = node;
  }
  appendDownNode(node) {
    this.downNode = node;
  }
  calcPos(pos, pos2) {
    return {
      x: pos[0] + pos2[0],
      y: pos[1] + pos2[1],
    };
  }

  _createNewNode(imageData, pos, index) {
    return new GraphNode({
      r: imageData.data[index],
      g: imageData.data[index + 1],
      b: imageData.data[index + 2],
      a: imageData.data[index + 3],
      x: pos.x,
      y: pos.y,
    });
  }
  setNextNode(imageData, x, y, index) {
    let pos = this.calcPos([x, y], [1, 0]);

    let node = this._createNewNode(imageData, pos, index);
    this.nextNode = node;
    node.upNode = this.findNode("up", this.calcPos([x, y], [0, -1]));
    node.downNode = this.findNode("down", this.calcPos([x, y], [0, 1]));
    node.preNode = this;

    return this.nextNode;

    // node = this.findNode('up',pos)
    // if(node){
    //     this.upNode = node
    // }

    // pos = this.calcPos([x,y],[0,1])
    // node = this.findNode('down',pos)
    // if(node){
    //     this.downNode = node
    // }
    // pos = this.calcPos([x,y],[-1,0])
    // node = this.findNode('pre',pos)
    // if(node){
    //     this.preNode = node
    // }
    // pos = this.calcPos([x,y],[1,0])
    // node = this.findNode('next',this.calcPos([x,y],[1,0]))
    // if(node){
    //     this.nextNode = node
    // }
  }
}
export class Graph {
  datas = [];
  constructor(opt) {
    this.datas = opt.imageData.data;
  }

  drawGrid(draw) {
    let count =0
  
    for (let i = 0; i < this.datas.length; i+=4) {
        let rgba ={
            r:this.datas[i],g:this.datas[i+1],b:this.datas[i+2],a:this.datas[i+3]
        }

      if(rgba.r===0&&rgba.g===0&&rgba.b===0&&rgba.a===0){
        count++
        continue}
      let x = count % draw.opt.width;
      let y = (count - x) / draw.opt.width;

      draw.drawRect({x, y,w: 1,h: 1,color:"#ff0000",lineWidth:1});
      count++
    }
  }
  static setGraphByImageData(imageData, width, height) {
    // console.log(imageData.data.length)
    return new Graph({
      imageData,
    });

    // let count = 1;
    // let gNode = new GraphNode({
    //         r:imageData.data[0],g:imageData.data[1],b:imageData.data[2],a:imageData.data[3],x:0,y:0
    // })

    // for (let i = 4; i < imageData.data.length; i += 4) {

    //   let x = count % width;
    //   let y = (count - x) / width;
    //   gNode=gNode.setNextNode(imageData,x,y,i)
    //   debugger
    //   count++

    // }

    // return gNode

    // console.log( g.glist)
  }
}
